//
//  KDKeywordNameController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/8.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDKeywordNameController.h"
#import "KDMainwordAddView.h"
#import "KDMainWordAddViewModel.h"
#import "KDNoticeView.h"
#import "KDEditeCellModel.h"
#define CELL_HEIGHT 40
#define NOTICE_LINE_HEIGHT 21

@interface KDKeywordNameController ()<KDMainwordAddViewDelegate, UINavigationControllerDelegate>
/**
 *  主要的View
 */
@property (nonatomic, weak) KDMainwordAddView *mainView;
/**
 *  注意的View
 */
@property (nonatomic, weak) KDNoticeView *noticeView;


@end

@implementation KDKeywordNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackGroundColor;
    [self setUpNoticeView];
    [self setUpMainView];
    [self setUpNavBar];
    //添加手势
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    [hide addTarget:self action:@selector(hideTheKeyBoard)];
    [self.view addGestureRecognizer:hide];
}

- (void)setUpMainView{
    KDMainwordAddView *mainView = [KDMainwordAddView wordView];
    self.mainView = mainView;
    mainView.delegate = self;
    KDMainWordAddViewModel *model1 = [[KDMainWordAddViewModel alloc]init];
    model1.wordTfPlaceHolder = (self.name.length)? self.name:@"请输入监测名称";
    KDEditeCellModel *model = [[KDEditeCellModel alloc]init];
    model.name = self.name;
    model1.infoArr = (self.name.length) ? @[model]:@[];
    model1.single = YES;
    self.mainView.model = model1;
    self.mainView.frame = CGRectMake(0, 10, screenW, CELL_HEIGHT * (mainView.model.infoArr.count +1));

    [self.view addSubview:mainView];
}
- (void)setUpNavBar{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewClick)];
}
- (void)setUpNoticeView{
    KDNoticeView *noticeView = [KDNoticeView noticeView];
    noticeView.frame = CGRectMake(0, CELL_HEIGHT +10, screenW, NOTICE_LINE_HEIGHT + 15);
    noticeView.title = @"请输入监测主题的名称！";
    [self.view addSubview:noticeView];
}
#pragma mark - 事件点击区
/**
 *  返回按钮被点击了
 */
- (void)backViewClick{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - KDMainwordAddViewDelegate
- (void)KDMainwordAddView:(KDMainwordAddView *)mainWordView HeightChangeWithIfAdd:(BOOL)add AndHeight:(CGFloat)height{
    if (_delegate && [_delegate respondsToSelector:@selector(nameController:finishedEditWith:)]) {
        KDEditeCellModel *model;
        if (self.mainView.model.infoArr.count) {
            model = self.mainView.model.infoArr[0];
        }
        [_delegate nameController:self finishedEditWith:model.name];
    }
}
#pragma mark - 隐藏键盘
- (void)hideTheKeyBoard
{
    [self.view endEditing:YES];
}
@end
